using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class Airport : MonoBehaviour {

    private static int tempPlaneCount = 1;
    public List<Aircraft> inboundPlanes = new List<Aircraft>();
    public List<Aircraft> outboundPlanes = new List<Aircraft>();

    public List<Aircraft> PlanesReadyForTakeOff {
        get {
            return outboundPlanes.Where(p => p.flightPhase == Aircraft.FlightPhase.departing) as List<Aircraft>;
        }
    }

    public Constants.Locations airportName;
    private void Start() {
        //Name the airport
        gameObject.name = airportName.ToString() + " Airport";
        //fix the draw order of the airports
        transform.position = new Vector3(transform.position.x, Constants.DRAW_ORDER_AIRPORT, transform.position.z);
        //Spawn a plane 
        PrepPlaneForTakeOff();
    }

	private void PrepPlaneForTakeOff(){
        GameObject newPlaneGO = GameObject.Instantiate(Resources.Load("Aeroplane")) as GameObject;        
        Aircraft newPlane = (newPlaneGO as GameObject).GetComponent<Aircraft>();
        outboundPlanes.Add(newPlane);
        newPlane.departureAirport = this;
        newPlane.DetermineDestinationAirport();
        newPlane.flightPhase = Aircraft.FlightPhase.departing;
        newPlaneGO.transform.position = transform.position;
        newPlaneGO.transform.name = "Plane_" + tempPlaneCount++;
	}

    public List<Aircraft> planesInHolding = new List<Aircraft>();
    public IEnumerator EnterHoldingPattern(Aircraft aeroplane) {
        GameObject newHoldingPattern = new GameObject("Holding_" + aeroplane.DestinationAirport.ToString());
        newHoldingPattern.transform.position = aeroplane.destinationAirport.gameObject.transform.position;
        newHoldingPattern.transform.parent = aeroplane.destinationAirport.gameObject.transform;
        newHoldingPattern.transform.LookAt(aeroplane.gameObject.transform);
        planesInHolding.Add(aeroplane);
        int holdingPos = planesInHolding.Count;
        while (aeroplane.flightPhase == Aircraft.FlightPhase.holding) {
            Vector3 holdingPatternLocation = newHoldingPattern.transform.position + (newHoldingPattern.transform.forward * holdingPos);
            newHoldingPattern.transform.RotateAroundLocal(Vector3.up, -0.005f);
            //Stay in Holding Pattern
            aeroplane.gameObject.transform.position 
                = Vector3.MoveTowards(aeroplane.gameObject.transform.position,
                                      holdingPatternLocation,
                                      aeroplane.speed * Time.deltaTime);

            //Look in the direction of flight
            aeroplane.transform.LookAt(transform.position);
            aeroplane.transform.LookAt(aeroplane.transform.position + aeroplane.transform.right);
            //find and orient the plane as it circles
            yield return new WaitForSeconds(0.01f);
        }
    }

}
